package org.jeecg.modules.gyy.gyycomment.service.impl;

import org.jeecg.modules.gyy.gyycomment.entity.GyyComment;
import org.jeecg.modules.gyy.gyycomment.entity.GyyReply;
import org.jeecg.modules.gyy.gyycomment.mapper.GyyReplyMapper;
import org.jeecg.modules.gyy.gyycomment.mapper.GyyCommentMapper;
import org.jeecg.modules.gyy.gyycomment.service.IGyyCommentService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 评论表
 * @Author: jeecg-boot
 * @Date:   2025-05-25
 * @Version: V1.0
 */
@Service
public class GyyCommentServiceImpl extends ServiceImpl<GyyCommentMapper, GyyComment> implements IGyyCommentService {

	@Autowired
	private GyyCommentMapper gyyCommentMapper;
	@Autowired
	private GyyReplyMapper gyyReplyMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(GyyComment gyyComment, List<GyyReply> gyyReplyList) {
		gyyCommentMapper.insert(gyyComment);
		if(gyyReplyList!=null && gyyReplyList.size()>0) {
			for(GyyReply entity:gyyReplyList) {
				//外键设置
				entity.setCommentId(gyyComment.getId());
				gyyReplyMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(GyyComment gyyComment,List<GyyReply> gyyReplyList) {
		gyyCommentMapper.updateById(gyyComment);

		//1.先删除子表数据
		gyyReplyMapper.deleteByMainId(gyyComment.getId());

		//2.子表数据重新插入
		if(gyyReplyList!=null && gyyReplyList.size()>0) {
			for(GyyReply entity:gyyReplyList) {
				//外键设置
				entity.setCommentId(gyyComment.getId());
				gyyReplyMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		gyyReplyMapper.deleteByMainId(id);
		gyyCommentMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			gyyReplyMapper.deleteByMainId(id.toString());
			gyyCommentMapper.deleteById(id);
		}
	}

}
